summaryrefslogtreecommitdiff
path: root/app/api/data-room/[projectId]/route.ts
blob: 643dcf0f90d42147b5c6aa8f23c9841b08e8b616 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// app/api/data-room/[projectId]/route.ts
import { NextRequest, NextResponse } from 'next/server';
import { getServerSession } from 'next-auth/next';
import { authOptions } from '@/app/api/auth/[...nextauth]/route'
import { FileService, type FileAccessContext } from '@/lib/services/fileService';
import { z } from 'zod';

// 파일 생성 스키마 검증
const createFileSchema = z.object({
  name: z.string().min(1).max(255),
  type: z.enum(['file', 'folder']),
  parentId: z.string().uuid().optional().nullable(),
  category: z.enum(['public', 'restricted', 'confidential', 'internal']).default('confidential'),
  mimeType: z.string().optional(),
  size: z.number().optional(),
  filePath: z.string().optional(),
});

// 파일 목록 조회
export async function GET(
  request: NextRequest,
  { params }: { params: { projectId: string } }
) {
  try {
    const session = await getServerSession(authOptions);
    if (!session?.user) {
      return NextResponse.json({ error: '인증이 필요합니다' }, { status: 401 });
    }

    const searchParams = request.nextUrl.searchParams;
    const parentId = searchParams.get('parentId');
    const viewMode = searchParams.get('viewMode'); // 'tree' or 'grid'
    const includeAll = searchParams.get('includeAll') === 'true'; // 전체 목록 가져오기

    const context: FileAccessContext = {
      userId: Number(session.user.id),
      userDomain: session.user.domain || 'partners',
      userEmail: session.user.email,
      ipAddress: request.ip || request.headers.get('x-forwarded-for') || undefined,
      userAgent: request.headers.get('user-agent') || undefined,
    };

    const fileService = new FileService();
    
    // viewMode가 tree이거나 includeAll이 true인 경우 전체 목록 가져오기
    const files = await fileService.getFileList(
      params.projectId,
      parentId,
      context,
      { 
        includeAll: viewMode === 'tree' || includeAll 
      }
    );

    return NextResponse.json(files);
  } catch (error) {
    console.error('파일 목록 조회 오류:', error);
    return NextResponse.json(
      { error: '파일 목록을 불러올 수 없습니다' },
      { status: 500 }
    );
  }
}

// 파일/폴더 생성
export async function POST(
  request: NextRequest,
  { params }: { params: { projectId: string } }
) {
  try {
    const session = await getServerSession(authOptions);
    if (!session?.user) {
      return NextResponse.json({ error: '인증이 필요합니다' }, { status: 401 });
    }

    const body = await request.json();
    const validatedData = createFileSchema.parse(body);

    const context: FileAccessContext = {
      userId: Number(session.user.id),
      userDomain: session.user.domain || 'partners',
      userEmail: session.user.email,
      ipAddress: request.ip || request.headers.get('x-forwarded-for') || undefined,
      userAgent: request.headers.get('user-agent') || undefined,
    };

    const fileService = new FileService();
    const newFile = await fileService.createFileItem(
      {
        ...validatedData,
        projectId: params.projectId,
      },
      context
    );

    return NextResponse.json(newFile, { status: 201 });
  } catch (error) {
    if (error instanceof z.ZodError) {
      return NextResponse.json(
        { error: '잘못된 요청 데이터', details: error.errors },
        { status: 400 }
      );
    }
     
    if (error instanceof Error && error.message === '권한이 없습니다') {
      return NextResponse.json(
        { error: '파일 생성 권한이 없습니다' },
        { status: 403 }
      );
    }

    console.error('파일 생성 오류:', error);
    return NextResponse.json(
      { error: '파일 생성에 실패했습니다' },
      { status: 500 }
    );
  }
}